# -*- coding: utf-8 -*-

# Copyright 2013 Ida Leter, adapted from Fanficdownloader
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import time
import logging
logger = logging.getLogger(__name__)
import re
import urllib2

from .. import BeautifulSoup as bs
from .. import exceptions as exceptions
from ..htmlcleanup import stripHTML

from base_adapter import BaseSiteAdapter,  makeDate

def getClass():
    return ArxivAdapter


logger = logging.getLogger(__name__)

class ArxivAdapter(BaseSiteAdapter):

    def __init__(self, config, url):
        BaseSiteAdapter.__init__(self, config, url)

        self.decode = ["utf8","Windows-1252"]
        self.username = "" # if left empty, site doesn't return any message at all.
        self.password = ""
        
        # get paperId
        self.paper.setMetadata('paperId',self.parsedUrl.path.split('/',)[2].split('v')[0])
        logger.debug("paperId: (%s)"%self.paper.getMetadata('paperId'))
        
        # normalized URL.
        self._setURL('http://' + self.getSiteDomain() + '/abs/'+self.paper.getMetadata('paperId'))
        
        # Each adapter needs to have a unique site abbreviation.
        self.paper.setMetadata('siteabbrev','arXiv')

        # The date format will vary from site to site.
        # http://docs.python.org/library/datetime.html#strftime-strptime-behavior
        self.dateformat = "%Y/%m/%d"
            
    @staticmethod # must be @staticmethod, don't remove it.
    def getSiteDomain():
        # The site domain.  Does have www here, if it uses it.
        return 'arxiv.org'

    def getSiteExampleURLs(self):
        return "http://arxiv.org/abs/1234.1234 http://arxiv.org/pdf/1234.1234v1.pdf"

    def getSiteURLPattern(self):
        return r"http://arxiv.org/(abs|pdf)?/\d+.\d+(v\d+)?"

    def extractMetadata(self):

        try:
            data = self._fetchUrl(self.url)
            
        except urllib2.HTTPError, e:
            if e.code == 404:
                raise exceptions.PaperDoesNotExist()
            else:
                raise e
            
        # use BeautifulSoup HTML parser to make everything easier to find.
        soup = bs.BeautifulSoup(data)

        # Now go hunting for all the meta data and the chapter list.
        
        # Title
        a = soup.find('meta', {'name' : 'citation_title'})
        self.paper.setMetadata('title',a['content'])
		
        # Author
        alist = soup.findAll('a', href=re.compile(r"/find/\w+(-\w+)?/1/au:"))
        for a in alist:
            self.paper.addToList('author',unicode(stripHTML(a)))
            self.paper.addToList('authorUrl','http://arxiv.org'+a['href'])

        # Date
        a = soup.find('meta', {'name' : 'citation_date'})
        self.paper.setMetadata('datePublished', makeDate(a['content'], self.dateformat))
        
        # Abstract
        a = soup.find('blockquote', {'class' : 'abstract'})
        a.find('span').extract()
        self.paper.setMetadata('abstract',stripHTML(a))
        
        # Comments
        
        alist = soup.find('td', {'class' : 'tablecell comments'})
        if alist != None:
            for a in alist.text.replace('.',',').split(', '):
                self.paper.addToList('tags',a)
        
        # Category
        alist = stripHTML(soup.find('td', {'class' : 'tablecell subjects'})).split('; ')
        for  a in alist:
            self.paper.addToList('tags',a)
            
        # ArXiv id
        self.paper.setMetadata('arxiv',self.paper.getMetadata('paperId'))
        self.paper.setMetadata('pdfurl','http://arxiv.org/pdf/%s.pdf' %self.paper.getMetadata('arxiv'))
        
        # Publisher
        self.paper.setMetadata('publisher','arXiv:')
        